package com.gonsin.ta.logic.parse.condition.context;

import cn.hutool.core.bean.BeanUtil;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.gonsin.ta.logic.enums.TaResult;
import com.google.gson.annotations.Expose;
import org.springframework.context.ApplicationContext;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * 执行事件上下文
 */
public class ChainContextImpl implements ChainContext {

    /**
     * 执行过程中的变量
     */
    private Map<String, Object> values = new HashMap<>();

    /**
     * 流程实例是否结束
     */
    private boolean end = false;

    /**
     * 实例id
     */
    private String instanceId;

    /**
     * 流程id
     */
    private String chainId;

    /**
     * 路由结果
     */
    private String switchResult;

    /**
     * 正在等待的信号
     */
    private String waitingSignal;

    /**
     * 执行结果
     */
    private TaResult result;

    // applicationContext无法序列化
    @JsonIgnore
    @Expose(serialize = false, deserialize = false)
    private transient ApplicationContext applicationContext;

    public ChainContextImpl(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public ChainContextImpl() {
    }

    public ChainContextImpl(ChainContextPo po) {
        BeanUtil.copyProperties(po, this);
    }

    /**
     * 强制结束流程
     * 已执行的内容不会改变，后续未执行的内容不会再执行
     */
    public void endChain(){
        this.end = true;
    }

    public String getInstanceId() {
        return instanceId;
    }

    public String getChainId() {
        return chainId;
    }

    public void setChainId(String chainId) {
        this.chainId = chainId;
    }

    public void setInstanceId(String instanceId) {
        this.instanceId = instanceId;
    }

    public boolean isEnd() {
        return end;
    }

    public String getSwitchResult() {
        return switchResult;
    }

    public void setSwitchResult(String switchResult) {
        this.switchResult = switchResult;
    }

    public TaResult getResult() {
        return result;
    }

    public void setResult(TaResult result) {
        this.result = result;
    }

    public String getWaitingSignal() {
        return waitingSignal;
    }

    public void setWaitingSignal(String waitingSignal) {
        this.waitingSignal = waitingSignal;
    }

    /**
     * 查询数据
     */
    public <T> T getValue(String key){
        return (T) values.get(key);
    }

    /**
     * 查询数据
     */
    public void putValue(String key, Serializable value){
        values.put(key, value);
    }

    public void setValues(Map<String, Serializable> values) {
        if(this.values == null){
            this.values = new HashMap<>();
        }
        this.values.putAll(values);
    }

    public Map<String, Object> getValues() {
        return values;
    }

    @Override
    public Object getBean(String beanName) {
        return applicationContext.getBean(beanName);
    }

    @Override
    public Object getBean(Class beanClass) {
        return applicationContext.getBean(beanClass);
    }

    public void setApplicationContext(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    public ChainContextPo getContextPo() {
        ChainContextPo contextPo = new ChainContextPo();
        BeanUtil.copyProperties(this, contextPo);
        return contextPo;
    }
}
